package com.jwthhealth_pub.old.acupressure.view;

import android.app.Activity;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;


import com.jwthhealth_pub.R;
import com.jwthhealth_pub.common.view.TitleLayout;
import com.jwthhealth_pub.old.acupressure.module.AcupressureJztzjlModule;
import com.jwthhealth_pub.old.acupressure.module.AcupressureJztzjldetailModule;
import com.jwthhealth_pub.old.acupressure.view.adapter.NineConstitutionDescAdapter;
import com.jwthhealth_pub.old.acupressure.view.adapter.NineConstitutionMainDescAdapter;
import com.jwthhealth_pub.old.api.ApiHelper;
import com.jwthhealth_pub.old.common.Constant;
import com.jwthhealth_pub.old.common.ImageLoader;

import butterknife.BindView;
import butterknife.ButterKnife;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by Administrator on 2017/1/12.
 */
public class NineConstitutionActivity extends Activity {
    @BindView(R.id.green)
    View view;
    @BindView(R.id.sec_title)
    TextView secTitle;
    @BindView(R.id.img)
    ImageView img;
    @BindView(R.id.main_desc_name)
    TextView mainDescName;
    @BindView(R.id.rv_main_desc)
    RecyclerView rvMainDesc;
    @BindView(R.id.desc_name)
    TextView descName;
    @BindView(R.id.rv_desc)
    RecyclerView rvDesc;
    @BindView(R.id.progressbar)
    ProgressBar progressbar;
    @BindView(R.id.sign_topbar)
    TitleLayout signTopbar;
    private AcupressureJztzjldetailModule.DataBean data;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_nineconstitution);
        ButterKnife.bind(this);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        }

        AcupressureJztzjlModule.DataBean dataBean = (AcupressureJztzjlModule.DataBean) getIntent().getExtras().getSerializable(Constant.ACUPRESSUREDATA);

        Call<AcupressureJztzjldetailModule> acupressureJztzjldetailModuleCall = ApiHelper.acupressureJztzjlDetail(dataBean.getId());
        acupressureJztzjldetailModuleCall.enqueue(new Callback<AcupressureJztzjldetailModule>() {
            @Override
            public void onResponse(Call<AcupressureJztzjldetailModule> call, Response<AcupressureJztzjldetailModule> response) {
                AcupressureJztzjldetailModule body = response.body();
                if (body.getCode().equals("0")) {
                    data = body.getData();
                    handler.sendEmptyMessage(0);
                } else {
                    Log.d("NineConstitutionActivit", "fail");
                }
            }

            @Override
            public void onFailure(Call<AcupressureJztzjldetailModule> call, Throwable t) {
                Log.d("NineConstitutionActivit", "error");
            }
        });
    }

    private void initTop() {
//        signTopbar.setTitle(data.getName());
        signTopbar.setOnTitleLayoutListener(new TitleLayout.TitleLayoutListener() {
            @Override
            public void leftclick() {
                finish();
            }

            @Override
            public void leftTextClick() {

            }

            @Override
            public void rightclick() {

            }

            @Override
            public void rightTextClick() {

            }
        });
    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        Log.d("MeridianInfoActivity", "img.getHeight():" + img.getHeight());
        Log.d("MeridianInfoActivity", "img.getWidth():" + img.getWidth());
        super.onWindowFocusChanged(hasFocus);
    }


    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == 0) {
                progressbar.setVisibility(View.GONE);
                initTop();
                initDate();
            }
        }
    };

    private void initDate() {
        secTitle.setText(data.getName());
//        Picasso.with(this).load(data.getThumb()).into(img);
        ImageLoader.picasso(data.getThumb(), img);
        mainDescName.setText(data.getMain_desc_name());
        descName.setText(data.getDesc_name());
        rvMainDesc.setLayoutManager(new LinearLayoutManager(this));
        rvMainDesc.setAdapter(new NineConstitutionMainDescAdapter(this, data.getMain_desc()));
        rvDesc.setLayoutManager(new LinearLayoutManager(this));
        rvDesc.setAdapter(new NineConstitutionDescAdapter(this, data.getDesc()));
    }
}
